﻿@using System.ComponentModel;
@using System.Collections.Concurrent;
@inject IStringLocalizer<Foo> FooLocalizer

<Table TItem="Foo"
        IsPagination="true" PageItemsSource="@PageItemsSource"
        IsStriped="true" IsBordered="true"
        ShowToolbar="true" ShowDefaultButtons="false"
        OnQueryAsync="@OnQueryAsync" ShowDetailRow="ShowDetailRow">
    <TableColumns>
        <TableColumn @bind-Field="@context.DateTime" Width="180" />
        <TableColumn @bind-Field="@context.Name" Width="100" />
        <TableColumn @bind-Field="@context.Address" />
        <TableColumn @bind-Field="@context.Count" />
    </TableColumns>
    <DetailRowTemplate>
        <Table TItem="DetailRow" IsBordered="true" ShowToolbar="false" Items="@GetDetailDataSource(context)">
            <TableColumns Context="Detail">
                <TableColumn @bind-Field="@Detail.Name" />
                <TableColumn @bind-Field="@Detail.DateTime" Width="180" />
                <TableColumn @bind-Field="@Detail.Complete" Width="70" />
            </TableColumns>
        </Table>
    </DetailRowTemplate>
</Table>

@code {
    private ConcurrentDictionary<string, List<DetailRow>> Cache { get; } = new();

    private static readonly Random random = new();

    private static IEnumerable<int> PageItemsSource => new int[] { 4, 10, 20 };

    [NotNull]
    private List<Foo>? Items { get; set; }

    /// <summary>
    /// OnInitialized 方法
    /// </summary>
    protected override void OnInitialized()
    {
        base.OnInitialized();

        Items = Foo.GenerateFoo(FooLocalizer);
    }

    private static List<DetailRow> GetDetailRowsByName(string name) => Enumerable.Range(1, 4).Select(i => new DetailRow()
    {
        Id = i,
        Name = name,
        DateTime = DateTime.Now.AddDays(i - 1),
        Complete = random.Next(1, 100) > 50
    }).ToList();


    private Task<QueryData<Foo>> OnQueryAsync(QueryPageOptions options)
    {
        IEnumerable<Foo> items = Items;

        // 设置记录总数
        var total = items.Count();

        // 内存分页
        items = items.Skip((options.PageIndex - 1) * options.PageItems).Take(options.PageItems).ToList();

        return Task.FromResult(new QueryData<Foo>()
        {
            Items = items,
            TotalCount = total
        });
    }

    private List<DetailRow> GetDetailDataSource(Foo foo)
    {
        var cacheKey = foo.Name ?? "";
        return Cache.GetOrAdd(cacheKey, key => GetDetailRowsByName(cacheKey));
    }

    private class DetailRow
    {
        [DisplayName("主键")]
        public int Id { get; set; }

        [DisplayName("培训课程")]
        [AutoGenerateColumn(Order = 10)]
        public string Name { get; set; } = "";

        [DisplayName("日期")]
        [AutoGenerateColumn(Order = 20, Width = 180)]
        public DateTime DateTime { get; set; }

        [DisplayName("是/否")]
        [AutoGenerateColumn(Order = 30, Width = 70)]
        public bool Complete { get; set; }
    }

    private static bool ShowDetailRow(Foo item) => item.Complete;
}
